package no.la58.scribbles.server.storage;

import java.util.ArrayList;

import no.la58.scribbles.shared.GetScribbleRequest;
import no.la58.scribbles.shared.Scribble;
import no.la58.scribbles.shared.ScribbleList;
import no.la58.scribbles.shared.VoidMethodStatus;

public class ScribbleStorage extends Storage<Scribble> {

    public VoidMethodStatus storeScribble(Scribble toStore) {
	Scribble storeObject = super.storeObject(toStore);

	if (storeObject != null) {
	    return VoidMethodStatus.SUCCESS;
	} else {
	    return VoidMethodStatus.ERROR;
	}
    }

    public ScribbleList getScribbles(GetScribbleRequest request) {
	ScribbleList result = new ScribbleList();

	String filter = null;
	if (request.getMaximumAge() > 0) {
	    filter = "timeStamp > " + request.getMaximumAge();
	}
	ArrayList<Scribble> scribbleArrayList = super.getObjectList(Scribble.class,
		request.getFromRange(), request.getToRange(), "timeStamp descending", filter);
	for (Scribble scribble : scribbleArrayList) {
	    scribble.setKey(null);
	}
	result.setScribbles(scribbleArrayList);

	return result;
    }

}
